package com.jingdianjichi.subject.domain.service.Imp;

import com.jingdianjichi.subject.common.enums.CategoryTypeEnums;
import com.jingdianjichi.subject.common.enums.IsDeleteFlagEnum;
import com.jingdianjichi.subject.domain.convert.SubjectLabelConvert;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBO;
import com.jingdianjichi.subject.domain.entity.SubjectMappingBO;
import com.jingdianjichi.subject.domain.service.SubjectLabelDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import com.jingdianjichi.subject.infra.basic.entity.SubjectLabel;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMapping;
import com.jingdianjichi.subject.infra.basic.service.SubjectLabelService;
import com.jingdianjichi.subject.infra.basic.service.impl.SubjectCategoryServiceImpl;
import com.jingdianjichi.subject.infra.basic.service.impl.SubjectLabelServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.print.DocFlavor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
public class SubjectLabelDomainServiceImp implements SubjectLabelDomainService {
    @Resource
    private SubjectLabelServiceImpl subjectLabelServiceImpl;
    @Autowired
    private SubjectCategoryServiceImpl subjectCategoryService;
    @Autowired
    private SubjectLabelServiceImpl subjectLabelService;

    /**
     * 新增标签
     * @param subjectLabelBO
     * @return
     */
    @Override
    public Boolean add(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelConvert.INSTANCE.convertBoToCategory(subjectLabelBO);
        int count=subjectLabelServiceImpl.add(subjectLabel);
        return count>0;
    }

    /**
     * 修改标签
     * @param subjectLabelBO
     * @return
     */
    @Override
    public Boolean update(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelConvert.INSTANCE.convertBoToCategory(subjectLabelBO);
        int count=subjectLabelServiceImpl.update(subjectLabel);
        return count>0;
    }

    /**
     * 标签删除
     * @param subjectLabelBO
     * @return
     */
    @Override
    public Boolean delete(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelConvert.INSTANCE.convertBoToCategory(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeleteFlagEnum.DELETED.getCode());
        int count=subjectLabelServiceImpl.update(subjectLabel);
        return count>0;
    }

    /**
     * 根据分类查询标签
     * @param subjectLabelBO
     * @return
     */
    @Override
    public List<SubjectLabelBO> queryByCategoryId(SubjectLabelBO subjectLabelBO) {
        SubjectCategory subjectCategory=subjectCategoryService.queryById(subjectLabelBO.getCategoryId());

        if(CategoryTypeEnums.PRIMARY.getCode()==subjectCategory.getCategoryType()){
            SubjectLabel subjectLabel=new SubjectLabel();
            subjectLabel.setCategoryId(subjectLabelBO.getCategoryId());
            List<SubjectLabel> labelList= subjectLabelService.queryByContion(subjectLabel);
            List<SubjectLabelBO> subjectLabelBOS = SubjectLabelConvert.INSTANCE.convertLabelListToBo(labelList);
            return subjectLabelBOS;
        }
        Long categoryId = subjectLabelBO.getCategoryId();
        SubjectMapping subjectMapping=new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeleteFlagEnum.UN_DELETED.getCode());
        List<SubjectMapping> mappingList =subjectLabelServiceImpl.queryByCategoryId(subjectMapping);

        if(CollectionUtils.isEmpty(mappingList)){
            return Collections.emptyList();
        }
        //流式编程 创造出label List
        List<Long> labelIdList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        List<SubjectLabel> labelList = subjectLabelServiceImpl.batchQueryById(labelIdList);


//        List <SubjectLabelBO> bolist=SubjectLabelConvert.INSTANCE.convertListBoToListLabel(labelList);
        List<SubjectLabelBO> bolist = new LinkedList<>();

        labelList.forEach(label -> {
            SubjectLabelBO bo = new SubjectLabelBO();
            bo.setId(label.getId());
            bo.setLabelName(label.getLabelName());
            bo.setCategoryId(categoryId);
            bo.setSortNum(label.getSortNum());
            bolist.add(bo);
        });
        return bolist;
    }
}
